package net.betou.trade.main.dao.impl;

import java.util.List;

import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.trade.entity.main.SuggestionType;
import net.betou.trade.main.dao.SuggestionTypeDao;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

@Repository
public class SuggestionTypeDaoImpl extends HibernateBaseDao< SuggestionType, Integer > implements SuggestionTypeDao{

	@Override
	protected Class< SuggestionType > getEntityClass( ) {
		return SuggestionType.class;
	}

	public SuggestionType deleteById( Integer id ) {
		SuggestionType bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}
		
		return bean;
	}

	public SuggestionType findById( Integer id ) {
		return get(id);
	}

	public SuggestionType save( SuggestionType bean ) {
		log.info(String.valueOf(bean.getTypeId()));
		log.info(bean.getName());
		log.info(bean.getDescription());
		log.info(String.valueOf(bean.getSort()));
		getSession( ).save( bean );
		return bean;
	}

	@SuppressWarnings("unchecked")
	public List<SuggestionType> findList(String orderProperty, String orderDirection) {
		DetachedCriteria detached = DetachedCriteria.forClass( getEntityClass( ) );
		if(!StringUtils.isBlank(orderProperty)){
			Order order;
			if(StringUtils.isBlank(orderDirection) || orderDirection.equals("desc")){
				order = Order.desc(orderProperty);
			}else{
				order = Order.asc(orderProperty);
			}
			detached.addOrder(order);
		}
		detached.addOrder(Order.asc("sort"));
		Criteria criteria = detached.getExecutableCriteria(getSession());
		return criteria.list();
	}
}